package com.jzlx.utils;

import com.alibaba.fastjson2.JSON;
import com.jzlx.domain.*;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;

import javax.swing.*;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;

/**
 * @description:
 * @author: zhulei
 * @create: 2025-03-24 15:14
 **/
public class OSHIUtil {
    private static SystemInfo systemInfo;

    private static SystemInfo getSystemInfo() {
        if (systemInfo == null)
            systemInfo = new SystemInfo();
        return systemInfo;
    }
    public static CpuInfo getCpuInfo() {
        SystemInfo systemInfo = getSystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();

        // 获取CPU信息
        CentralProcessor processor = hardware.getProcessor();
        double cpuUsage = processor.getSystemCpuLoad(1000) * 100; // CPU使用率（百分比）
        System.out.println("CPU Usage: " + String.format("%.2f", cpuUsage) + "%");

        // 获取 CPU 型号
        String cpuModel = processor.getProcessorIdentifier().getName(); // CPU 型号
        System.out.println("CPU Model: " + cpuModel);

        return new CpuInfo(cpuUsage,cpuModel);

    }

    public static MemoryInfo getMemoryInfo(String unit) {
        long divisor = 1L;
        if (MemoryInfo.GB.equals(unit)){
            divisor = 1024L*1024L*1024L;
        }
        if (MemoryInfo.MB.equals(unit)){
            divisor = 1024L*1024L;
        }
        if (MemoryInfo.KB.equals(unit)){
            divisor = 1024L;
        }
        SystemInfo systemInfo = getSystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        // 获取内存信息
        GlobalMemory memory = hardware.getMemory();
        long totalMemory = memory.getTotal() / divisor; // 总内存（MB）
        long availableMemory = memory.getAvailable() / divisor; // 可用内存（MB）
        System.out.println("Total Memory: " + totalMemory + unit);
        System.out.println("Available Memory: " + availableMemory + unit);
        return new MemoryInfo(totalMemory,availableMemory,unit);
    }

    public static DiskSpaceInfo getDiskSpaceInfo(String unit) {
        long divisor = 1L;
        if (DiskSpaceInfo.GB.equals(unit)){
            divisor = 1024L*1024L*1024L;
        }
        if (DiskSpaceInfo.MB.equals(unit)){
            divisor = 1024L*1024L;
        }
        if (DiskSpaceInfo.KB.equals(unit)){
            divisor = 1024L;
        }
        SystemInfo systemInfo = getSystemInfo();
        OperatingSystem os = systemInfo.getOperatingSystem();
        // 获取磁盘信息
        long finalDivisor = divisor;
        long totalDiskSpace = os.getFileSystem().getFileStores().stream()
                .mapToLong(store -> store.getTotalSpace() / finalDivisor) // 总磁盘空间（GB）
                .sum();
        long usableDiskSpace = os.getFileSystem().getFileStores().stream()
                .mapToLong(store -> store.getUsableSpace() / finalDivisor) // 可用磁盘空间（GB）
                .sum();
        System.out.println("Total Disk Space: " + totalDiskSpace + unit);
        System.out.println("Usable Disk Space: " + usableDiskSpace + unit);
        return new DiskSpaceInfo(totalDiskSpace,usableDiskSpace,unit);
    }

    public static OperatingSystemInfo getOperatingSystemInfo() {
        SystemInfo systemInfo = getSystemInfo();
        OperatingSystem os = systemInfo.getOperatingSystem();
        // 获取系统名称
        String osName = os.getFamily(); // 操作系统家族（如 Windows、Linux）
        String osVersion = os.getVersionInfo().getVersion(); // 操作系统版本
        System.out.println("Operating System: " + osName + " " + osVersion);
        return new OperatingSystemInfo(osName,osVersion);
    }


    public static List<ProcessInfo> getProcessesList() {
        LinkedList<ProcessInfo> processInfos = new LinkedList<>();
        SystemInfo systemInfo = getSystemInfo();
        OperatingSystem os = systemInfo.getOperatingSystem();
        // 获取系统名称
        List<OSProcess> processes = os.getProcesses();
        for (OSProcess process:
        processes) {
            ProcessInfo info = new ProcessInfo();
            info.setProcessId(process.getProcessID());
            info.setProcessName(process.getName());
            info.setProcessPath(process.getPath());
            info.setProcessMemory(process.getVirtualSize());
            info.setProcessDisk(process.getResidentSetSize());
            info.setProcessCpuLoad(process.getProcessCpuLoadCumulative());
            processInfos.add(info);
        }
        System.out.println(JSON.toJSONString(processInfos));
        return processInfos;
    }
}
